package cn.edu.zafu.bigdata.lhff;

import java.io.*;
import java.util.Scanner;

public class FileOperations {

    public static void writeToFile(String fileName, String content) {
        try {
            FileWriter fileWriter = new FileWriter(fileName);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(content);
            bufferedWriter.close();
            System.out.println("内容已成功写入文件: " + fileName);
        } catch (IOException e) {
            System.out.println("写入文件时出错: " + e.getMessage());
        }
    }

    public static void readFromFile(String fileName) {
        try {
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;
            System.out.println("文件内容：");
            while ((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }
            bufferedReader.close();
        } catch (IOException e) {
            System.out.println("读取文件时出错: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入学号: ");
        String studentID = scanner.nextLine();

        String fileName = "files/" + studentID + ".txt";

        System.out.print("请输入要写入文件的内容: ");
        String content = scanner.nextLine();

        writeToFile(fileName, content);
        readFromFile(fileName);

        scanner.close();
    }
}
